package movie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import movie.pojo.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import movie.service.MovieService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieController {

    //controller层调用service层
    //service层调用mapper层

    @Autowired
    private MovieService movieService;

    @GetMapping("/index")
    public String index(HttpServletRequest req,String name){
        List<Movie> movieList = null;
        //针对name进行校验
        if(null!=name && name.trim().length()!=0){
            //name查询条件是存在的.
            //构建条件对象
            QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name",name);

            movieList = movieService.list(queryWrapper);

            //为了让查询条件进行数据的回显操作
            req.setAttribute("name",name);
        }else{
            //获取product表中所有的数据
            movieList = movieService.list();
        }

        //将数据库返回出来的数据放入到request作用域中
        //key-value形式
        req.setAttribute("movieList",movieList);

        //转发到jsp
        return "movie/index";
    }
}
